package com.nazca.tvremote.event;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.hardware.Sensor;
import android.hardware.SensorEvent;

public class RemoteSensorEvent extends RemoteEvent {

	private SensorEvent mSensorEvent;
	public RemoteSensorEvent(SensorEvent sensorEvent){
		mSensorEvent = sensorEvent;
	}

	@Override
	public String toString() {
		JSONObject json = new JSONObject();
		try {
			json.put("EventType", "SensorEvent");
		
			JSONObject event = new JSONObject();
			json.put("Event", event);
			
			event.put("accuracy", mSensorEvent.accuracy);
			event.put("timestamp", mSensorEvent.timestamp);
			
			JSONArray valuesJO = new JSONArray();
			event.put("values", valuesJO);
			
			JSONObject sensorJO = new JSONObject();
			event.put("sensor", sensorJO);
			
			float[] values = mSensorEvent.values;
			for(float value : values){
				valuesJO.put(String.valueOf(value));
			}
			
			Sensor sensor = mSensorEvent.sensor;
			sensorJO.put("MaximumRange", sensor.getMaximumRange());
			sensorJO.put("Name", sensor.getName());
			sensorJO.put("Power", sensor.getPower());
			sensorJO.put("Resolution", sensor.getResolution());
			sensorJO.put("Type", sensor.getType());
			sensorJO.put("Vendor", sensor.getVendor());
			sensorJO.put("Version", sensor.getVersion());
		}catch (JSONException e) {
			e.printStackTrace();
		}
		return json.toString();
	}
	
}
